package org.guitycrown.Command.NetworkCommand;

import io.airlift.airline.Command;
import io.airlift.airline.Option;
import org.guitycrown.CommandGroup.NetworkGroup.SimpleNetwork;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created by kira on 2016/5/27.
 */

/**
 * Usage:
 * network ping -s 127.0.0.1
 * network ping -s 127.0.0.1 -c 5
 * network ping -s 127.0.0.1 -c 5 -t 100000
 */
@Command(name = "ping", description = "ping remote server")
public class PingCommand extends SimpleNetwork implements Runnable {
    @Option(name = "-s", description = "target server")
    public String serverIP;

    @Option(name = "-t", description = "ping server timeout,default is 3s", required = false)
    public int timeOut = 3000;

    @Option(name = "-c", description = "how much time ping the server,default is 5", required = false)
    public int pingCount = 5;

    public void run() {
        if (help) {
            System.out.println("Help Text");
            return;
        }

        for (int i = 0; i < pingCount; i++) {
            try {
                InetAddress address = InetAddress.getByName(serverIP);
                boolean reachable = address.isReachable(timeOut);
                if (reachable) {
                    System.out.println("Host Can Reach");
                } else {
                    System.out.println("Host is UnReachable");
                }
            } catch (UnknownHostException e) {
                System.out.println("Host is Unknown");
            } catch (IOException e) {
                System.out.println("Host is UnReachable");
            }
        }

    }
}
